﻿using Autofac;
using Microsoft.Extensions.DependencyInjection;

namespace Puppy.Autofac
{
    public static class AutofacApplicationCreationOptionsExtensions
    {
        public static void UseAutofac(this PuppyApplicationOptions options)
        {
            options.Services.AddAutofacServiceProviderFactory();
        }

        public static PuppyAutofacServiceProviderFactory AddAutofacServiceProviderFactory(this IServiceCollection services)
        {
            return services.AddAutofacServiceProviderFactory(new ContainerBuilder());
        }

        public static PuppyAutofacServiceProviderFactory AddAutofacServiceProviderFactory(this IServiceCollection services, ContainerBuilder containerBuilder)
        {
            var factory = new PuppyAutofacServiceProviderFactory(containerBuilder);

            services.AddObjectAccessor(containerBuilder);
            services.AddSingleton((IServiceProviderFactory<ContainerBuilder>)factory);

            return factory;
        }
    }
}
